﻿using ThresholdAssistant.DataContract.Result;

namespace ThresholdAssistant.DataContract.Extensions
{
    /// <summary>
    /// 商家接口返回扩展
    /// </summary>
    public static class MerchantContractExtensions
    {
        /// <summary>
        /// 转换为商户返回结果
        /// </summary>
        /// <param name="result"></param>
        /// <returns></returns>
        public static MerchantContractResult ToResult(this ContractResult result)
        {
            var response = new MerchantContractResult();
            if (result == null)
            {
                return response;
            }

            int.TryParse(result.ErrorCode, out var errorCode);

            response.SerialNo = result.SerialNo;
            response.ErrorCode = errorCode;
            response.ErrorMessage = result.ErrorMessage;
            return response;
        }

        /// <summary>
        /// 转换为商户返回结果
        /// </summary>
        /// <param name="result"></param>
        /// <returns></returns>
        public static MerchantContractResult ToResult<T>(this ContractResult<T> result)
        {
            var response = new MerchantContractResult<T>();
            if (result == null)
            {
                return response;
            }

            int.TryParse(result.ErrorCode, out var errorCode);

            response.Data = result.Data;
            response.SerialNo = result.SerialNo;
            response.ErrorCode = errorCode;
            response.ErrorMessage = result.ErrorMessage;
            return response;
        }
    }
}